package project

import (
	"context"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/business_opportunity"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"strings"
)

type landService struct {
	projectRepo business_opportunity.ProjectRepositoryIface
}

// NewLandService 土地
func NewLandService(
	projectRepo business_opportunity.ProjectRepositoryIface,
) LandServiceIface {
	return &landService{
		projectRepo: projectRepo,
	}
}

func (s *landService) GetTakenLand(ctx context.Context, request *dto.GetTakenLandRequest) (*dto.GetTakeLandResponse, error) {
	requestPb := &pb.GetTakeLandListRequest{
		OrganizingCode:    request.OrganizingCode,
		Keyword:           request.Keyword,
		AreaCode:          request.AreaCode,
		TakeLandDateStart: request.TakeLandDateStart,
		TakeLandDateEnd:   request.TakeLandDateEnd,
		LandUsage:         request.LandUsage,
		Page:              request.Page,
		PageSize:          request.PageSize,
		LandAreaMin:       request.LandAreaMin,
		LandAreaMax:       request.LandAreaMax,
		LandPriceMin:      request.LandPriceMin,
		LandPriceMax:      request.LandPriceMax,
		CompanyAreaCode:   request.CompanyAreaCode,
		CompanyType:       request.CompanyType,
		CompanyLabel:      request.CompanyLabel,
	}
	if len(request.PriceGtAvgLabel) == 1 {
		if request.PriceGtAvgLabel[0] == "高端项目" {
			requestPb.IsPriceGtAvg = "是"
		} else if request.PriceGtAvgLabel[0] == "非高端项目" {
			requestPb.IsPriceGtAvg = "否"
		}
	}

	resPb, err := s.projectRepo.GetTakenLand(ctx, requestPb)
	res := &dto.GetTakeLandResponse{
		MaxNum: 5000,
		List:   []*dto.GetTakenLandItem{},
	}
	if err != nil {
		return res, err
	}
	res.Total = resPb.Total
	for _, item := range resPb.List {
		takenLandItem := &dto.GetTakenLandItem{
			OrganizingCode:    item.OrganizingCode,
			DeveloperName:     item.DeveloperName,
			DeveloperNameHl:   item.DeveloperNameHl,
			SubOrganizingCode: item.SubOrganizingCode,
			SubDeveloperName:  item.SubDeveloperName,
			ProvinceCode:      item.ProvinceCode,
			Province:          item.Province,
			CityCode:          item.CityCode,
			City:              item.City,
			CreatedAt:         item.CreatedAt,
			LandTitle:         item.LandTitle,
			LandTitleHl:       item.LandTitleHl,
			LandUsage:         strings.Split(item.LandUsageLabel, ","),
			LandAddress:       item.LandAddress,
			AllArea:           item.AllArea,
			TakeLandDate:      item.TakeLandDate,
			TakeLandPrice:     item.TakeLandPrice,
			InfoType:          item.InfoType,
			IssueDate:         item.IssueDate,
			Id:                item.Id,
			ProjectName:       item.ProjectName,
			PriceGtAvgLabel:   item.PriceGtAvgLabel,
		}
		res.List = append(res.List, takenLandItem)
	}
	return res, nil
}

func (s *landService) ExportTakenLand(ctx context.Context, request *dto.GetTakenLandRequest) (*dto.ExportTakeLandResponse, error) {
	requestPb := &pb.ExportTakeLandListRequest{
		OrganizingCode:    request.OrganizingCode,
		Keyword:           request.Keyword,
		AreaCode:          request.AreaCode,
		TakeLandDateStart: request.TakeLandDateStart,
		TakeLandDateEnd:   request.TakeLandDateEnd,
		LandUsage:         request.LandUsage,
		LandAreaMin:       request.LandAreaMin,
		LandAreaMax:       request.LandAreaMax,
		LandPriceMin:      request.LandPriceMin,
		LandPriceMax:      request.LandPriceMax,
		CompanyAreaCode:   request.CompanyAreaCode,
		CompanyType:       request.CompanyType,
		CompanyLabel:      request.CompanyLabel,
	}
	if len(request.PriceGtAvgLabel) == 1 {
		if request.PriceGtAvgLabel[0] == "高端项目" {
			requestPb.IsPriceGtAvg = "是"
		} else if request.PriceGtAvgLabel[0] == "非高端项目" {
			requestPb.IsPriceGtAvg = "否"
		}
	}
	resPb, err := s.projectRepo.ExportTakenLand(ctx, requestPb)
	res := &dto.ExportTakeLandResponse{}
	if err != nil {
		return res, err
	}
	res.JobId = resPb.JobId
	return res, nil
}
